package LawsonTransformJava.pgm;
// OracleDAOPaemergcnt implementation of the 
// EmployeeDAO interface. This class can contain all
// Oracle specific code and SQL statements. 
// The client is thus shielded from knowing 
// these implementation details.

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*;
import java.lang.reflect.InvocationTargetException;

import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAOPAEmergCnt;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblPAEmergCnt;

public class PA512 extends DAObject implements 
    DAOPAEmergCnt, Runnable {

	private TblPAEmergCnt peg = null;

    public PA512(File output, File errOutput, File errReport) {
    // initialization 
		try {
			this.output = new FileWriter(this.outputFile = output);
			this.errOutput = new FileWriter(this.errOutputFile = errOutput);
			this.errReport = new Report(errReport, new Date());

			//initialization
			init();
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
  }

  private void init() throws IOException {
	    // Set the data variables
	    data = DAOFactoryOracleDB.getDAOOracleDB(true);
	    
		errReport.setPgmName(this.getClass().getSimpleName());

		sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
	    stmtData = data.getDataStatement();	    
	    countStmtData = data.getCountStatement();
	    //Set the Date Format for output
	    df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  }
  
  public TblPAEmergCnt findPAEmergCnt() {
		try {
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + " " 
					+ (whereClause==null?"":whereClause);
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			if (rslt.next()) {
				hasMoreRecs = true;
				peg = (TblPAEmergCnt) createObject(rslt, new TblPAEmergCnt());
			} else
				hasMoreRecs = false;
			return peg;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

  public TblPAEmergCnt nextPaemergcnt() {

		try {
			if (rslt.next()) {
				hasMoreRecs = true;
				peg = (TblPAEmergCnt) createObject(rslt, new TblPAEmergCnt());
			} else
				hasMoreRecs = false;
			return peg;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

	public boolean hasMore () {
	  return hasMoreRecs;
	}
 
  public int countPAEmergCnt() {
	try {
		countRecs = data.getCountStatement().executeQuery("SELECT COUNT(*) " +
				"FROM " + data.getSchema() + "." + TABLE_NAME + " " + 
				(whereClause==null?"":whereClause));
		countRecs.next();
		return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
  }
  
  public void run() {
	  try {
		//Update current date/time
		System.out.println(errReport.getName() + " Start " + Report.getDateFormat().format(new Date()));
		
		peg = findPAEmergCnt();
		while (hasMore()) {
			writePA512Layout(peg);
			peg = nextPaemergcnt();
		}
		//Update current date/time
		System.out.println(errReport.getName() + " Finish " + Report.getDateFormat().format(new Date()));

		//Write Record Counts
		errReport.writeRecordCounts(this.countPAEmergCnt());
		
		//Reclaim Resources
		fileCleanup();

	} catch (IOException e) {
		// Could not write properties file
		// Could not create output file
		System.out.println("IOException: " + e);
	}

  }

	//Output the PA512 format
	public void writePA512Layout(TblPAEmergCnt peg) throws IOException {

		String writeValue = null;

		writeValue =
				"\""+"A"+"\"" + "," +	
				"1" + "," +
				peg.getEmployee() + "," +
				peg.getSeq_nbr() + "," +
				peg.getLast_name() + "," +
				peg.getFirst_name() + "," +
                peg.getMiddle_init() + "," +
				peg.getAddr1() + "," +
				peg.getAddr2() + "," +
				peg.getAddr3() + "," +
				peg.getAddr4() + "," +
				peg.getCity() + "," +
				peg.getState() + "," +
				peg.getZip() + "," +
				peg.getCountry_code() + "," +
				peg.getRelationship() + "," +
				peg.getCl_phone_cntry() + "," +
				peg.getCl_phone_nbr() + "," +
				peg.getHm_phone_cntry() + "," + 
				peg.getHm_phone_nbr() + "," +
				peg.getWk_phone_cntry() + "," +
				peg.getWk_phone_nbr() + "," +
				peg.getWk_phone_ext() + "," +
				peg.getDate_stamp() + "," +
				peg.getTime_stamp() + "," +
				peg.getUser_id() 				
			;
		
		//Increment Record Written
		errReport.addWrittenCnt();
		//Write to Load File
		this.output.write(writeValue + "\n");
		this.output.flush();
	}

	public void fileCleanup() throws IOException {
		errReport.flush();
		errReport.close();
		output.flush();
		output.close();
		errOutput.flush();
		errOutput.close();
	}
}